package com.example.a1.fannong.core.app;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 整个应用程序基类，所有Activity都需要继承本类
 */
public  class BaseActivity extends FragmentActivity {
    public static final String TAG = "Activity";
    /**
     * 日志打印
     */
    protected Logger logger = new Logger(this);
    private ProgressDialog loadingDialog;

    protected Handler getHandler() {
        return MyApplication.HANDLER;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().injectManager(this);
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    protected int getResColor(@ColorRes int res) {
        return getResources().getColor(res);
    }

    /**
     * 显示输入法
     */
    public void showSoftInput(EditText editText) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
    }

    public void hideSoftInput(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    // 请使用 FindViewById
    @Deprecated
    @Override
    public View findViewById(int id) {
        return super.findViewById(id);
    }

    public void startActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent(this, cls);
        super.startActivity(intent);
    }

    /**
     * DIP 转 PX
     *
     * @param dip
     * @return
     */
    public static int dipToPx(float dip) {
        return dipToPx(MyApplication.getInstance(), dip);
    }

    public static int dipToPx(Context context, float dip) {
        return (int) (context.getResources().getDisplayMetrics().density * dip + 0.5f);
    }

    public void onBackClick(View view) {
        finish();
    }

    public BaseActivity getActivity() {
        return this;
    }

    public final Drawable getResDrawable(@DrawableRes int id) {
        return getResources().getDrawable(id);
    }

    private ProgressDialog createProgressDialog() {
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setIndeterminate(false);

        progressDialog.setCancelable(false);
        return progressDialog;
    }

    /**
     * 显示正在加载中对话框
     */
    public void showLoadingDialog() {
        showLoadingDialog("正在加载中...");
    }

    /**
     * 显示自定义文字对话框
     */
    public void showLoadingDialog(String msg) {
        if (isFinishing()) {
            return;
        }
        loadingDialog = createProgressDialog();
        loadingDialog.setMessage(msg);
        loadingDialog.show();
    }

    /**
     * 隐藏对话框
     */
    public void dismissLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    @Override
    public void finish() {
        super.finish();
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    public final Context getContext() {
        return this;
    }

    /**
     * 获得管理器
     *
     * @param manager 管理器类型
     * @param <M>     管理器Class
     * @return 管理器
     */
    public <M extends BaseManager> M getManager(Class<M> manager) {
        return MyApplication.getInstance().getManager(manager);
    }


}
